﻿

Public Class FrmSector
    Dim classController As New Class_RC_Controller.rc_Mantenimiento
    Private _codSector As Integer = 0

    Public Property codSector() As Integer
        Get
            Return _codSector
        End Get
        Set(ByVal value As Integer)
            _codSector = value
        End Set
    End Property
    Private Sub FrmSector_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.llenararea()
        Me.CargarDatos()
    End Sub

    Private Sub CargarDatos()
        Try
            If codSector > 0 Then

                Dim ds As New DataTable
                ds = classController.get_sector_All(oiml.dbConn, codSector, "", 0)

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("codSector")
                    txtdescripcion.Text = ds.Rows(0)("descSector")
                    txtobsector.Text = ds.Rows(0)("ObsSector")
                    btneliminar.Enabled = True
                    cmbarea.SelectedValue = ds.Rows(0)("codarea").ToString
                End If
            Else
                btneliminar.Enabled = False
                txtdescripcion.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub
    Private Sub llenararea()
        Dim dsArea As DataTable
        dsArea = classController.get_Area_All(oiml.dbConn, 0, "")
        'If Not dsArea Is Nothing Then
        '    If dsArea.Rows.Count > 0 Then
        '        cmbarea.DataSource = dsArea
        '        cmbarea.DisplayMember = "descArea"
        '        cmbarea.ValueMember = "codArea"
        '    End If
        'End If
        With Me.cmbarea
            .DataSource = dsArea
            .DisplayMember = "descArea"
            .ValueMember = "codArea"
        End With
    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If

        If codSector = 0 Then
            codSector = classController.Insert_Sector(oiml.dbConn, txtdescripcion.Text, cmbarea.SelectedValue, txtobsector.Text)
            If codSector > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Sector", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Sector", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_Sector(oiml.dbConn, codSector, txtdescripcion.Text, cmbarea.SelectedValue, txtobsector.Text) Then
                MessageBox.Show("Registro grabado con exito!", "Sector", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Sector", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub


    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        If codSector > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Sector", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Sector(oiml.dbConn, codSector) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Sector", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub
End Class